import {Component} from '@angular/core';
import {InfiniteScroll, IonicPage, NavController, NavParams} from 'ionic-angular';
import {ConsigneeProvider} from "@providers/interface/consignee";
import {StorageProvider} from "@providers/storage";
import {PagingConfig} from "@providers/paging";

/**
 * Generated class for the ConsigneeFinishPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-consignee-finish',
  templateUrl: 'consignee-finish.html',
})
export class ConsigneeFinishPage {

  consignee: any = 'not';
  userInfo: any = this.storage.read("userInfo");
  yets: any[] = [];
  yetConfige: any;
  nots: any[] = [];
  notConfige: any;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private storage:StorageProvider,
              private consigneeP: ConsigneeProvider) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ConsigneeFinishPage');
  }

  ionViewWillEnter() {
    this.getNot();
    this.getYet();
  }

  getNot() {
    this.nots = [];
    this.notConfige = new PagingConfig();
    if (this.nots.length == 0) {
      this.getData((value) => {
        this.nots = value;
        console.log(this.nots);
      }, this.notConfige, '0');
    }
  }

  getYet() {
    this.yets= [];
    this.yetConfige = new PagingConfig();
    if (this.yets.length == 0) {
      this.getData((value) => {
        this.yets= value;
        console.log(this.yets);
      }, this.yetConfige, '1');
    }
  }

  /**
   * 获取数据
   * @param DJZT=CS 状态 CS=0表示已提货未装车 CS=1表示已提货已装车
   * @param pagingConfig 分页
   * @param fn 方法
   */
  private getData(fn: (value) => void, pagingConfig, DJZT?: string) {
    if (pagingConfig.isEnd) {
      return;
    }
    this.consigneeP.getLadingFinish({
      THYBM:this.userInfo.PARTYID,
      THYMC:this.userInfo.USERNAME,
      CS: DJZT
    }, pagingConfig)
      .then((data) => {
        fn(data);
        console.info(data);
      }).catch(() => {
    });
  }

  getMoreNotDatas(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      infiniteScroll.complete();
      if (this.notConfige.isEnd) {
        infiniteScroll.enable(false);
      }
      this.nots.push.apply(this.nots, value);
    }, this.notConfige, '0');
  }

  getMoreYetDatas(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      infiniteScroll.complete();
      if (this.yetConfige.isEnd) {
        infiniteScroll.enable(false);
      }
      this.yets.push.apply(this.yets, value);
    }, this.yetConfige, '1');
  }
}
